package com.example.fengqiqi

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class AddStudentActivity : AppCompatActivity() {

    private lateinit var fileHelper: FileHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_student)

        fileHelper = FileHelper(this)

        val idEditText: EditText = findViewById(R.id.edit_text_id)
        val nameEditText: EditText = findViewById(R.id.edit_text_name)
        val genderEditText: EditText = findViewById(R.id.edit_text_gender)
        val classEditText: EditText = findViewById(R.id.edit_text_class)
        val addButton: Button = findViewById(R.id.button_add)
        val cancelButton: Button = findViewById(R.id.button_cancel)
        addButton.setOnClickListener {
            val student = Student(
                idEditText.text.toString(),
                nameEditText.text.toString(),
                genderEditText.text.toString(),
                classEditText.text.toString()
            )
            val students = fileHelper.loadStudents().toMutableList()
            students.add(student)
            fileHelper.saveStudents(students)
            finish()
        }
        cancelButton.setOnClickListener {
            idEditText.text.clear()
            nameEditText.text.clear()
            genderEditText.text.clear()
            classEditText.text.clear()
            finish()
        }
    }
}